from perceptron import votedron
from util import read_plain, kernel, dot
from random import shuffle
import sys

"""
    Simple usage example

    Runs the regular perceptron and the voted perceptron = the votedron.
    
"""
    
def main(arg=None):
    
       
    train_file = arg[0]
    d = int(arg[1])
    num_seconds = int(arg[2])
    num_runs = int(arg[3])
    
    input_instances = read_plain(train_file)
    
    shuffle(input_instances)
    
    num_input_instances = len(input_instances)
    #print "Read", train_file[train_file.rindex('/')+1:], 'with',num_input_instances , 'instances'
    train_instances = input_instances[:int(num_input_instances*0.9)]
    test_instances = input_instances[int(num_input_instances*0.9):]
    
    p = votedron([0.0 for i in range(len(train_instances[0].features))],kernel(d) if d>1 else dot)
    
    #print 'Seconds','Perceptron','Votedron'
    
    for run in range(num_runs):
        
        p.train(train_instances, num_seconds)
        p_miss = p.test(test_instances)
        v_miss = p.test_voted(test_instances)

        print (run+1)*num_seconds, 1.0*p_miss/len(test_instances), 1.0*v_miss/len(test_instances)

if __name__ == "__main__":

    main(sys.argv[1:])
